<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
	</head>

	<body>
		<div class="x-body" id="app">
			<xblock class="layui-form">
				<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
				<button class="layui-btn" onclick="open_addPublicCourse()"><i class="layui-icon"></i>增加</button>
				<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
             title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
				<span class="x-right" style="line-height:40px">共有数据：{{ pageInfo.total }} 条，当前{{ pageInfo.pageNum }} / {{ pageInfo.pages }}页</span>
				<form id="import_excel_form" enctype="multipart/form-data">
                    <input type="file" hidden="hidden" name="excelFile" id="importInput" onchange="import_excel(this)" accept=".xlsx"/>
                </form>
			</xblock>
			<table class="layui-table">
				<thead>
					<tr>
						<th width="20">
							<div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
						</th>
						<th width="60">课程代码</th>
                        <th>课程名称</th>
                        <th width="60">学分</th>
                        <th width="60">课程性质</th>
                        <!-- <th width="60">排序号</th> -->
                        <th width="150">操作</th>
				</thead>
				<tbody>
					<tr v-for="publicCourse in publicCourses">
						<td>
							<div class="layui-unselect layui-form-checkbox" lay-skin="primary" :data-id="publicCourse.id" :data-courseName="publicCourse.courseName" ><i class="layui-icon">&#xe605;</i></div>
						</td>
						<td>{{ publicCourse.courseNo }}</td>
						<td>{{ publicCourse.courseName }}</td>
						<td>{{ publicCourse.credit }}</td>
						<!-- <td v-if="publicCourse.type == 3">公共选修</td> 
						<td>{{ publicCourse.sort }}</td>-->
						<td class="td-manage">
							<button class="layui-btn layui-btn layui-btn-sm" onclick="open_editPublicCourse(this)"><i class="layui-icon">&#xe642;</i>编辑</button>
							<button class="layui-btn-danger layui-btn layui-btn-sm" onclick="member_del(this,'要删除的id')" href="javascript:;"><i
								 class="layui-icon">&#xe640;</i>删除</button>
						</td>
					</tr>
				</tbody>
			</table>
			<div class="page" style="margin-top: 10px !important;">
               <div>
                 <a class="prev pageTxt" href="javascript:void(0)">上一页</a>
                 <a class="first pageTxt" href="javascript:void(0)">首页</a>
                 <template v-for="pageNum in pageInfo.navigatepageNums">
                     <a class="num" href="javascript:void(0)" v-if="pageInfo.pageNum != pageNum" :data="pageNum">{{ pageNum }}</a>
                     <span class="current" v-else :data="pageNum">{{ pageNum }}</span>
                 </template>
                 <a class="last pageTxt" href="javascript:void(0)">尾页</a>
                 <a class="next pageTxt" href="javascript:void(0)">下一页</a>
               </div>
             </div>
		</div>
		<script type="text/javascript" src="/js/jquery.min.js"></script>
        <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/js/xadmin.js"></script>
		<script type="text/javascript" src="/js/vue.min.js"></script>
        <script type="text/javascript" src="/js/jquery.form.js"></script>
		<script>
		
			var vm = new Vue({
	            el : '#app',
	            data : {
	            	publicCourses : "",
	                pageInfo:""
	            },
	            created : function() {
	                // 获取公选课
	                this.listPublicCourse(1);
	            },
	            methods : {
	            	listPublicCourse : function(pageNum) {
	                    var vm = this;
	                    $.ajax({
	                        url : "/sys/school/course/listPublicCourse",
	                        data:{
                                pageNum:pageNum
                            },
	                        type : "get",
	                        success : function(result) {
	                            if (result.state) {
	                            	vm.pageInfo = result.data;
	                            	vm.publicCourses = result.data.list;
	                            } else {
	                                layui.use(['layer'], function(){
	                                    var layer = layui.layer;
	                                    layer.msg(""+result.msg, {icon: 5});
	                                });
	                            }
	                        }
	                    });
	                }
	            }
	        });
		
			// 点击页码数，进行跳转
            $(".page").delegate(".num", "click", function() {
                var pageNum = $(this).attr("data");
                vm.listPublicCourse(pageNum);
            });
            // 上一页
            $(".page").delegate(".prev", "click", function() {
                if (vm.pageInfo.hasPreviousPage) {
                    vm.listPublicCourse(vm.pageInfo.pageNum - 1);
                }
            });
            // 下一页
            $(".page").delegate(".next", "click", function() {
                if (vm.pageInfo.hasNextPage) {
                    vm.listPublicCourse(vm.pageInfo.pageNum + 1);
                }
            });
            // 首页
            $(".page").delegate(".first", "click", function() {
                if (!vm.pageInfo.isFirstPage) {
                    vm.listPublicCourse(1);
                }
            });
            // 尾页
            $(".page").delegate(".last", "click", function() {
                if (!vm.pageInfo.isLastPage) {
                    vm.listPublicCourse(vm.pageInfo.pages);
                }
            });
            // 添加公选课
			function open_addPublicCourse() {
				x_admin_show('添加公共选修课程','/sys/school/course/toAddPublicCourse',600, 360);
			}
            // 编辑公选课
            function open_editPublicCourse(obj) {
            	var courseId = $(obj).parents("tr").find("div:first").attr("data-id");
                x_admin_show('编辑课程','/sys/school/course/toEditPublicCourse?courseId=' + courseId,600, 360)
            }

            /*单个删除*/
            function member_del(obj) {
                var courseName = $(obj).parents("tr").find("div:first").attr("data-courseName");
                var id = $(obj).parents("tr").find("div:first").attr("data-id");
                var courseId = [];
                courseId.push(id);
                layer.confirm('确认要删除【'+courseName+'】吗？', function(index) {
                     $.ajax({
                          url : "/sys/school/course/delCourse",
                          type : "post",
                          data : {
                              courseId: courseId
                          },
                          success : function(result) {
                              if (result.state) {
                                  layer.msg('删除成功!',{icon:1,time:1000});
                                  // 根据当前页面数据量决定加载前一页还是当前页
                                  if (vm.pageInfo.size - 1 == 0 && vm.pageInfo.pageNum != 1) {
                                      vm.listPublicCourse(vm.pageInfo.pageNum - 1);
                                  } else {
                                      vm.listPublicCourse(vm.pageInfo.pageNum);
                                  }
                              } else {
                                  layer.msg(""+result.msg, {icon: 5});
                              }
                          }
                      });
                });
            }

            // 批量删除
            function delAll(argument) {
                var courseId = tableCheck.getData();
                if (courseId.length <= 0) {
                    return;
                }
                // 获取选中的课程名称
                var obj = $(".layui-form-checked").not('.header');
                var courseName=[];
                obj.each(function(index, el) {
                    courseName.push(obj.eq(index).attr('data-courseName'));
                });
                layer.confirm('确认要删除【'+courseName+'】吗？', function(index) {
                    $.ajax({
                        url : "/sys/school/course/delCourse",
                        type : "post",
                        data : {
                            courseId: courseId
                        },
                        success : function(result) {
                            if (result.state) {
                                layer.msg('删除成功', {icon: 1});
                                // 移除所有checkbox的勾选状态
                                $(".layui-form-checkbox").removeClass('layui-form-checked');
                                
                                // 根据当前页面数据量决定加载前一页还是当前页
                                if (vm.pageInfo.size - courseId.length == 0 && vm.pageInfo.pageNum != 1) {
                                    vm.listPublicCourse(vm.pageInfo.pageNum - 1);
                                } else {
                                    vm.listPublicCourse(vm.pageInfo.pageNum);
                                }
                            } else {
                                layer.msg(""+result.msg, {icon: 5});
                            }
                        }
                    });
                });
            }
            // 保存后，更新页面数据
            function updateData() {
                vm.listPublicCourse(1);
            }
		</script>
	</body>

</html>
